package cn.iocoder.yudao.module.trade.dal.mysql.giftcard;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.controller.admin.giftcard.vo.CardPageReqVO;
import cn.iocoder.yudao.module.trade.controller.admin.order.vo.TradeOrderPageReqVO;
import cn.iocoder.yudao.module.trade.controller.app.giftcard.vo.AppGiftCardMemberVO;
import cn.iocoder.yudao.module.trade.controller.app.giftcard.vo.AppSomeVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.giftcard.GiftCardDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.order.TradeOrderDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@Mapper
public interface GiftCardMapper extends BaseMapperX<GiftCardDO> {

    AppGiftCardMemberVO queryMemberInfoByOpenId(String openid);

    default List<GiftCardDO> queryGiftCardByTradeOrderId(Long tradeOrderId) {
        return selectList(new LambdaQueryWrapperX<GiftCardDO>()
                .eq(GiftCardDO::getTradeOrderId, tradeOrderId)
                .orderByDesc(GiftCardDO::getCreateTime));
    }

    default List<GiftCardDO> queryGiftCardByStatusMapper(Integer status, Long memberUserId) {
        return selectList(new LambdaQueryWrapperX<GiftCardDO>()
                .eq(GiftCardDO::getCurrentOwnerMemberUserId, memberUserId)
                .eqIfPresent(GiftCardDO::getGiftCardStatus, status).orderByDesc(GiftCardDO::getCreateTime));
    }

    default int updateByIdAndStatus(Long id, GiftCardDO update, Integer status) {
        return update(update, new LambdaQueryWrapper<GiftCardDO>()
                .eq(GiftCardDO::getId, id).eq(GiftCardDO::getGiftCardStatus, status));
    }

    Long querySocialUserIdByUserId(Long userId);


    default PageResult<GiftCardDO> selectPage(CardPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<GiftCardDO>()
                .likeIfPresent(GiftCardDO::getGiftCardTitle, reqVO.getGiftCardTitle())
                .eqIfPresent(GiftCardDO::getWxCardId, reqVO.getWxCardId())
                .eqIfPresent(GiftCardDO::getTradeOrderId, reqVO.getTradeOrderId())
                .eqIfPresent(GiftCardDO::getBlessings, reqVO.getBlessings())
                .eqIfPresent(GiftCardDO::getGiftCardStatus, reqVO.getGiftCardStatus())
                .orderByDesc(GiftCardDO::getId));
    }

    AppSomeVO queryAppSomeVO(@Param("giftCardId") Long giftCardId);

    void updateGiftCardByOrderId(@Param("giftCardStatus") Integer giftCardStatus, @Param("orderId") Long orderId);

    Integer addPayWalletBalanceByUserId(@Param("balance") Integer balance, @Param("id") Long id);

}
